<?php
/**
 * ArticleToTag.php
 *
 * 日志和标签关联信息对象模型类.
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Momoger
 * @package 	Momoger_Model
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Model_Abstract */
//require_once 'Abstract.php';

/**
 * 日志和标签关联信息对象模型类.
 *
 * @category 	Momoger
 * @package		Momoger_Model
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class Momoger_Model_ArticleToTag extends Momoger_Model_Abstract {

	/**
	 * 标签对象模型
	 *
	 * @var Momoger_Model_Tag
	 */
	private $_tag = null;

	/**
	 * 日志对象模型
	 *
	 * @var Momoger_Model_Article
	 */
	private $_article = null;

	/**
	 * 构造函数.
	 *
	 * @param mixed $options 快速设置参数.
	 */
	public function __construct($options = null) {
        parent::__construct($options);
	}

	/**
	 * 获取日志相关的标签信息
	 *
	 * @param int $article_id 日志ID
	 * @return array
	 */
	public function getArticleTags($article_id) {
		$models = $this->findAll("articleID = $article_id");
		$tagIds = array();
		foreach ($models as $model) {
			$id = $model->get('tagID');
			$tagIds[$id] = $id;
		}
		$tags = array();
		if (count($tagIds)) {
			$models = $this->tag()->findAll("tagID IN ('" . implode("','", $tagIds) . "')");
			foreach ($models as $model) {
				$tags[$model->get('tagID')] = $model->getAll();
			}
		}
		return $tags;
	}

	/**
	 * 获取日志对象模型
	 *
	 * @return Momoger_Model_Article
	 */
	public function article() {
		if (null === $this->_article) {
			$this->_article = new Momoger_Model_Article();
		}
		return $this->_article;
	}

	/**
	 * 获取标签对象模型
	 *
	 * @return Momoger_Model_Tag
	 */
	public function tag() {
		if (null === $this->_tag) {
			$this->_tag = new Momoger_Model_Tag();
		}
		return $this->_tag;
	}

    /**
     * 获取数据地图对象.
     *
     * @return Momoger_Model_Mapper_ArticleToTag
     */
    protected function getMapper() {
        if (null === $this->_mapper) {
            $this->setMapper(new Momoger_Model_Mapper_ArticleToTag());
        }
        return $this->_mapper;
    }
}